package com.zlzxm.shoesmarket.view.activity;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.zlzxm.shoesmarket.R;
import com.zlzxm.shoesmarket.util.ZViewHelp;
import com.zlzxm.shoesmarket.view.activity.manage.ActivityIndex;
import com.zlzxm.shoesmarket.view.widget.simpleead.OnLeftclicklistener;
import com.zlzxm.shoesmarket.view.widget.simpleead.SimpleHead;
import com.zlzxm.shoesmarket.viewmodel.VMLogin;


/**
 * CREATE BY zlzxm ON 2021/5/11
 */
public class ACLogin extends BaseActivity implements View.OnClickListener , TextWatcher {

    public static void start(Context context) {
        Intent starter = new Intent(context, ACLogin.class);
        context.startActivity(starter);
    }

    VMLogin vmLogin;

    RelativeLayout rlCode, rlPwd;

    EditText etPhone,etCode,etPwd;

    TextView tvLogin,tvSignType;

    ImageView ivSee;

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.ac_login);
        SimpleHead simpleHead = ZViewHelp.findById(this,R.id.head);
        simpleHead.setTitle("");
        simpleHead.setOnItemclicklistener(new OnLeftclicklistener() {
            @Override
            public void onLeftItemClick() {
                finish();
            }
        });

        rlCode = ZViewHelp.findById(this,R.id.rlCode);

        rlPwd = ZViewHelp.findById(this,R.id.rlPwd);

        etPhone =  ZViewHelp.addTextChangedListener(this,R.id.etPhone,this);
        etCode = ZViewHelp.addTextChangedListener(this,R.id.etCode,this);
        etPwd = ZViewHelp.addTextChangedListener(this,R.id.etPwd,this);

        ivSee = ZViewHelp.setOnClickListener(this,R.id.ivSee,this);

        tvLogin =  ZViewHelp.setOnClickListener(this,R.id.tvLogin,this);

        tvSignType = ZViewHelp.setOnClickListener(this,R.id.tvSignType,this);
        ZViewHelp.setOnClickListener(this,R.id.tvSignUp,this);
    }


    @Override
    protected void initViewModule() {
        super.initViewModule();
        vmLogin = new ViewModelProvider(this).get(VMLogin.class);
        vmLogin.liveIsCodeLogin.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {

                if (aBoolean) {
                    rlCode.setVisibility(View.VISIBLE);
                    rlPwd.setVisibility(View.GONE);
                    tvSignType.setText("用密码登录");
                    showPassword(false);
                } else {
                    rlCode.setVisibility(View.GONE);
                    rlPwd.setVisibility(View.VISIBLE);
                    tvSignType.setText("用验证码登录");
                }
                etCode.setText(null);
                etPwd.setText(null);

            }
        });

        vmLogin.liveIsBtnEnable.observe(this,tvLogin::setEnabled);

        vmLogin.getLiveErrorToast().observe(this,this::errorToast);
        vmLogin.getLiveSucceedToast().observe(this,this::succeedToast);
    }

    @Override
    protected void initData() {
        super.initData();
    }

    @Override
    public void onClick(View view) {

        if (view.getId() == R.id.tvSignType) {

            changeSignType();

        }else if(view.getId() == R.id.tvSignUp){

            ActivityIndex.startACSignUp(this);

        }else if(view.getId() == R.id.ivSee){

            showPassword(ivSee.isSelected());

        }else if (view.getId() == R.id.tvLogin){

            vmLogin.requestLogin(etPhone.getText().toString(), etPwd.getText().toString());
        }
    }


    private void showPassword(boolean show){

        etPwd.setTransformationMethod(show? HideReturnsTransformationMethod.getInstance():
                PasswordTransformationMethod.getInstance());

        ivSee.setSelected(!show);
    }

    private void changeSignType() {

        vmLogin.liveIsCodeLogin.setValue(! vmLogin.liveIsCodeLogin.getValue());
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable editable) {

        vmLogin.updateInput(etPhone.getText().toString(),etCode.getText().toString(),etPwd.getText().toString());

    }
}
